package weeek11.topic10;

import com.sun.org.apache.regexp.internal.RE;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;

public class RedisLuaTest {
    public static void main(String[] args) {
        JedisPool jedisPool = JedisPoolUtil.getJedisPool();
        String user_usd_1 = "user_usd_1";
        String user_rmb_1 = "user_rmb_1";
        String user_usd_2 = "user_usd_2";
        String user_rmb_2 = "user_rmb_2";
        try (Jedis jedis = jedisPool.getResource()) {
            //init
            jedis.set(user_usd_1,String.valueOf( 10));
            jedis.set(user_rmb_1,String.valueOf( 10));
            jedis.set(user_usd_2,String.valueOf( 10));
            jedis.set(user_rmb_2,String.valueOf( 10));
            System.out.println("初始值:");
            System.out.println(user_usd_1+":"+jedis.get(user_usd_1));
            System.out.println(user_rmb_1+":"+jedis.get(user_rmb_1));
            System.out.println(user_usd_2+":"+jedis.get(user_usd_2));
            System.out.println(user_rmb_2+":"+jedis.get(user_rmb_2));

            //transaction
            String lua ="local userIdDecRmbAddUsd = KEYS[1];\n" +
                    "local userIdDecUsdAddRmb =KEYS[2];\n" +
                    "local user_usd_1 =  tonumber(redis.call('get','user_usd_' .. userIdDecRmbAddUsd));\n" +
                    "local user_rmb_1 =  tonumber(redis.call('get','user_rmb_' .. userIdDecRmbAddUsd));\n" +
                    "local user_usd_2 =  tonumber(redis.call('get','user_usd_' .. userIdDecUsdAddRmb));\n" +
                    "local user_rmb_2 =  tonumber(redis.call('get','user_rmb_' .. userIdDecUsdAddRmb));\n" +
                    "local rmb = tonumber(ARGV[1]);\n" +
                    "local usd = tonumber(ARGV[2]);\n" +
                    //判断账户是否充足
                    "if user_rmb_1 >= rmb and  user_usd_2 >= usd \n" +
                    "then \n" +
                    "redis.call('set','user_usd_' .. userIdDecRmbAddUsd,user_usd_1 + usd);\n" +
                    "redis.call('set','user_rmb_' .. userIdDecRmbAddUsd,user_rmb_1 - rmb);\n" +
                    "redis.call('set','user_usd_' .. userIdDecUsdAddRmb,user_usd_2 -usd);\n" +
                    "redis.call('set','user_rmb_' .. userIdDecUsdAddRmb,user_rmb_2 +rmb);\n" +
                    "return 1;"+
                    "end;\n" +
                    "return 0;";


            for (int i = 0; i < 2; i++) {

                //KEY
                //第一个参数的用户 减rmb 加usd
                //第二个参数的用户 加rmb 减usd
                //VALUE
                //第一个参数 RMB
                //第二个参数 USD
                Object eval = jedis.eval(lua, Arrays.asList("1", "2"), Arrays.asList("7", "1"));
                System.out.println("第"+(i+1)+"次结果:"+eval);
                System.out.println(user_usd_1+":"+jedis.get(user_usd_1));
                System.out.println(user_rmb_1+":"+jedis.get(user_rmb_1));
                System.out.println(user_usd_2+":"+jedis.get(user_usd_2));
                System.out.println(user_rmb_2+":"+jedis.get(user_rmb_2));
            }

        }
    }
}
